# Author: Michal 'mucha' Muszynski
# License: MIT

package Topic;
use base Module;
use strict;
use utf8;

my $topicsplitter = Config::get("topicsplitter", "||");

Module::define_command("rpush|push", \&on_rpush, 
	{
		usage => "<new_topic_part>",
		about => "Add new part to the topic, at right side",
		example => "!push \"Party at Sucki's house at 8!\"",
	}
);


Module::define_command("lpush|unshift", \&on_lpush, 
	{
		usage => "<new_topic_part>",
		about => "Add new part to the topic, at left side",
		example => "!push \"Don't choose lesser evil, choose Cthulhu\"",
	}
);

Module::define_command("lpop|shift", \&on_lpop,
	{
		about => "Remove part to the topic, at left side",
	}
);

Module::define_command("rpop|pop", \&on_rpop,
	{
		about => "Remove part to the topic, at right side",
	}
);



sub on_topic {

}

sub on_rpush {
	my ($nick, $options, @args) = @_;
	return if(!@args);

	my $topicpart = join(' ', @args);

	if (length(Core::topic) != 0) {
		Core::set_topic(Core::topic . " " . $topicsplitter . " " .  $topicpart);
	}
	else {
		Core::set_topic($topicpart);
	}
}

sub on_lpush {
	my ($nick, $options, @args) = @_;
	return if(!@args);

	my $topicpart = join(' ', @args);

	if (length(Core::topic) != 0) {
		Core::set_topic($topicpart . " " . $topicsplitter . " " . Core::topic);
	}
	else {
		Core::set_topic($topicpart);
	}
}

sub on_rpop {
	if (Core::topic =~ /((.*)\Q$topicsplitter\E) (.*)/) {
		my $tmp = $2;
		$tmp =~ s/ $//; #what is this for? :)
		Core::set_topic($tmp);
	}
}

sub on_lpop {
	if (Core::topic =~ /((.*?)\Q$topicsplitter\E )(.*)/) {
		Core::set_topic($3);
	}
}


1;
